<template>
    <view>
        <view class="main">
            <view class="top">
                <view class="inner" @tap="handleUserTap">
                    <view class="avatar">
                        <image src="/static/worktrain/tabbar/my_cur.png" mode="aspectFill"></image>
                    </view>
                    <view class="right">
                        <view class="line1">
                            <view class="name">{{ user ? user.USER_NAME : '注册 / 登录' }}</view>
                            <view v-if="user && user.USER_STATUS == 9" class="type bg-red">已禁用</view>
                            <view v-else-if="user && user.USER_STATUS == 0" class="type bg-orange">已注册，待审核</view>
                            <view v-else-if="user && user.USER_STATUS == 8" class="type bg-orange">审核未通过，请修改资料</view>
                        </view>
                        <view class="line2">{{ user ? '欢迎回来~~~' : '点击登录或注册' }}</view>
                    </view>
                </view>

                <view class="ex">
                    <view class="ex-inner">
                        <text class="icon-edit margin-right-xs"></text>
                        <view @tap="url" data-url="/pages/my/edit/my_edit" class="title">修改我的个人资料</view>
                    </view>
                </view>
            </view>

            <view class="meet-menu">
                <view class="padding-s text-bold">
                    <text class="icon-calendar margin-right-xxs"></text>
                    我的预约
                </view>
                <view class="inner">
                    <view class="item" @tap="url" data-url="/pages/meet/my_join_list/meet_my_join_list?status=use">
                        <view class="img">
                            <image src="/static/worktrain/my/use.png" />
                        </view>
                        <text>可使用</text>
                    </view>
                    <view class="item" @tap="url" data-url="/pages/meet/my_join_list/meet_my_join_list?status=check">
                        <view class="img">
                            <image src="/static/worktrain/my/check.png" />
                        </view>
                        <text>已核销</text>
                    </view>
                    <view class="item" @tap="url" data-url="/pages/meet/my_join_list/meet_my_join_list?status=timeout"> 
                        <view class="img">
                            <image src="/static/worktrain/my/out.png" />
                        </view>
                        <text>已过期</text>
                    </view>
                    <view class="item" @tap="url" data-url="/pages/meet/my_join_list/meet_my_join_list?status=cancel">
                        <view class="img">
                            <image src="/static/worktrain/my/cancel.png" />
                        </view>
                        <text>系统取消</text>
                    </view>
                    <view class="item" @tap="url" data-url="/pages/meet/my_join_list/meet_my_join_list?status=all">
                        <view class="img">
                            <image src="/static/worktrain/my/all.png" />
                        </view>
                        <text>全部预约</text>
                    </view>
                </view>
            </view>

            <view class="list-menu">
                <view class="item" @tap="url" data-url="/pages/my/fav/my_fav">
                    <view class="title">
                        <text class="icon-favor title-ico"></text>
                        <text>我的收藏</text>
                    </view>
                </view>
                <view class="item" @tap="url" data-url="/pages/my/foot/my_foot">
                    <view class="title">
                        <text class="icon-footprint title-ico"></text>
                        <text>历史浏览</text>
                    </view>
                </view>

                <view class="item" @tap="bindSetTap">
                    <view class="title">
                        <text class="icon-friend title-ico"></text>
                        <text style="color: var(--projectColor)">
                            老师入口
                            <text v-if="skin.IS_SUB" class="text-grey text-normal">（点击试用）</text>
                        </text>
                    </view>
                </view>

                <view class="item" v-if="skin.IS_SUB" @tap="bindSetTap">
                    <view class="title">
                        <text class="icon-settings title-ico"></text>
                        <text class="text-red text-bold">
                            系统后台管理
                            <text class="text-grey text-normal">（点击试用）</text>
                        </text>
                    </view>
                </view>

                <view class="item" @tap="url" data-url="/pages/about/index/about_index?key=SETUP_CONTENT_ABOUT">
                    <view class="title">
                        <text class="icon-service title-ico"></text>
                        <text>关于我们</text>
                    </view>
                </view>

                <view class="item" v-if="skin.IS_SUB" @tap="url" data-url="/pages/home/about/home_about">
                    <view class="title">
                        <text class="icon-phone title-ico"></text>
                        <text class="text-orange">联系作者了解更多详情</text>
                    </view>
                </view>

                <view class="item" v-if="!skin.IS_SUB" @tap="bindSetTap">
                    <view class="title">
                        <text class="icon-settings title-ico"></text>
                        <text>设置</text>
                    </view>
                </view>
            </view>
            <view style="height: 150rpx"></view>
        </view>

        <block v-if="skin.IS_SUB">
            <!-- parse <template is="menuTpl" :data="curMenu:'my_index',returnHome:false"/> -->
            <block name="menuTpl" v-if="false">
                <view class="tab-bar">
                    <view v-if="false" class="tab-bar-home" @tap="url" data-type="relaunch" data-url="/pages/home/index/home_index">
                        <text class="tab-bar-home-text icon-homefill text-grey"></text>
                    </view>

                    <view class="tab-bar-border"></view>
                    <view class="tab-bar-item" @tap="url" data-type="relaunch" data-url="/projects/worktrain/pages/default/index/default_index">
                        <image class="tab-icon" :src="'/projects/worktrain/images/tabbar/home' + ('home_index' == 'home_index' ? '_cur' : '') + '.png'"></image>
                        <view :class="'tab-text ' + ('home_index' == 'home_index' ? 'menu-project' : '')">首页</view>
                    </view>

                    <view class="tab-bar-item" @tap="url" data-type="relaunch" data-url="/projects/worktrain/pages/news/index/news_index">
                        <image class="tab-icon" :src="'/projects/worktrain/images/tabbar/news' + ('home_index' == 'news_index' ? '_cur' : '') + '.png'"></image>
                        <view :class="'tab-text ' + ('home_index' == 'news_index' ? 'menu-project' : '')">公告通知</view>
                    </view>

                    <view v-if="skin.cate2 !== false" class="tab-bar-item" @tap="url" data-type="relaunch" data-url="/projects/worktrain/pages/meet/calendar/meet_calendar">
                        <image class="tab-icon" :src="'/projects/worktrain/images/tabbar/day' + ('home_index' == 'meet_calendar' ? '_cur' : '') + '.png'"></image>
                        <view :class="'tab-text ' + ('home_index' == 'meet_calendar' ? 'menu-project' : '')">预约日历</view>
                    </view>

                    <view class="tab-bar-item" @tap="url" data-type="relaunch" data-url="/projects/worktrain/pages/my/index/my_index">
                        <image class="tab-icon" :src="'/projects/worktrain/images/tabbar/my' + ('home_index' == 'my_index' ? '_cur' : '') + '.png'"></image>
                        <view :class="'tab-text ' + ('home_index' == 'my_index' ? 'menu-project' : '')">个人中心</view>
                    </view>
                </view>
            </block>
        </block>
    </view>
</template>

<script>
/**
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux0730 (wechat)
 * Date: 2025-10-29 07:48:00
 */

const cacheHelper = require('@/utils/helper/cache_helper.js');
const pageHelper = require('@/utils/helper/page_helper.js');
const cloudHelper = require('@/utils/helper/cloud_helper.js');
const ProjectBiz = require('@/utils/biz/project_biz.js');
const AdminBiz = require('@/utils/comm/biz/admin_biz.js');
const WorkBiz = require('@/utils/biz/work_biz.js');
const setting = require('@/utils/setting/setting.js');
const PassportBiz = require('@/utils/comm/biz/passport_biz.js');
export default {
    data() {
        return {
            user: null,

            skin: {
                IS_SUB: '',
                cate2: false
            }
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: async function (options) {
        // if (PassportBiz.isLogin()) {
        //     let user = {};
        //     user.USER_NAME = PassportBiz.getUserName();
        //     this.setData({
        //         user
        //     });
        // }
        ProjectBiz.initPage(this);
        
        // 检查登录状态
        this.checkLoginStatus();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: async function () {
        //此处是一个伪造的登录
        // PassportBiz.loginSilenceMust(this);
        //加载个人用户信息
        // this.loadUserFun();
        
        // 检查登录状态
        this.checkLoginStatus();
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: async function () {
        await this.loadUserFun();
        uni.stopPullDownRefresh();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        checkLoginStatus: function () {
            console.log('检查登录状态');
            if (PassportBiz.isLogin()) {
                let user = {
                    USER_NAME: PassportBiz.getUserName(),
                    USER_STATUS: PassportBiz.getStatus()
                };
                console.log('用户已登录:', user);
                this.setData({
                    user
                });
            } else {
                console.log('用户未登录');
                this.setData({
                    user: null
                });
            }
        },

        loadUserFun: async function (e) {
            // let opts = {
            //     title: 'bar'
            // };
            // let user = await cloudHelper.callCloudData('passport/my_detail', {}, opts);
            // if (!user) {
            //     this.setData({
            //         user: null
            //     });
            //     return;
            // }
            // this.setData({
            //     user
            // });
        },

        url: function (e) {
            pageHelper.url(e, this);
        },

        handleUserTap: function (e) {
            console.log('handleUserTap called, user:', this.user);
            if (this.user) {
                // 已登录，跳转到编辑页面
                uni.navigateTo({
                    url: '/pages/my/edit/my_edit'
                });
            } else {
                // 未登录，显示登录注册选择
                this.bindLoginRegTap(e);
            }
        },

        bindLoginRegTap: function (e) {
            console.log('bindLoginRegTap', e);
            let itemList = ['登录', '注册'];
            uni.showActionSheet({
                itemList,
                success: (res) => {
                    let idx = res.tapIndex;
                    if (idx == 0) {
                        // 登录
                        uni.navigateTo({
                            url: '/pages/my/login/login'
                        });
                    }
                    if (idx == 1) {
                        // 注册
                        uni.navigateTo({
                            url: '/pages/my/reg/my_reg'
                        });
                    }
                },
                fail: function (res) {}
            });
        },

        bindSetTap: function (e, skin) {
            let itemList = ['清除缓存', '后台管理', '老师平台'];
            uni.showActionSheet({
                itemList,
                success: async (res) => {
                    let idx = res.tapIndex;
                    if (idx == 0) {
                        cacheHelper.clear();
                        pageHelper.showNoneToast('清除缓存成功');
                    }
                    if (idx == 1) {
                        if (setting.IS_SUB) {
                            AdminBiz.adminLogin(this, 'admin', '123456');
                        } else {
                            uni.reLaunch({
                                url: '/pages/admin/index/login/admin_login'
                            });
                        }
                    }
                    if (idx == 2) {
                        if (setting.IS_SUB) {
                            WorkBiz.workLogin(this, '13700000000', '123456');
                        } else {
                            uni.reLaunch({
                                url: '/pages/work/index/login/work_login'
                            });
                        }
                    }
                },
                fail: function (res) {}
            });
        }
    }
};
</script>
<style>
/* @import './my_index.css'; */
@import './my_index.css';
</style>
